layui.define(['table', 'jquery', 'element'], function(exports) {
	"use strict";

	var MOD_NAME = 'menu',
		$ = layui.jquery,
		element = layui.element;

	var pearMenu = function(opt) {
		this.option = opt;
	};

	pearMenu.prototype.render = function(opt) {

		var option = {
			elem: opt.elem,
			async: opt.async,
			parseData: opt.parseData,
			url: opt.url,
			method: opt.method ? opt.method : "GET",
			defaultOpen: opt.defaultOpen,
			defaultSelect: opt.defaultSelect,
			control: opt.control,
			defaultMenu: opt.defaultMenu,
			accordion: opt.accordion,
			height: opt.height,
			theme: opt.theme,
			data: opt.data ? opt.data : [],
			fields: opt.fields,
			token: opt.token,
			path: opt.rootPath,
			change: opt.change ? opt.change : function() {},
			done: opt.done ? opt.done : function() {}
		}
		if (option.async) {
			if (option.method === "GET") {
				getData(option.path!==null? option.path + option.url : option.url, option.token).then(function(data) {
					option.data = data;
					renderMenu(option);
				});
			} else {
				postData(option.path!==null? option.path + option.url : option.url, option.token, option.path).then(function(data) {
					option.data = data;
					renderMenu(option);
				});
			}
		} else {
			//renderMenu中需要调用done事件，done事件中需要menu对象，但是此时还未返回menu对象，做个延时提前返回对象
			window.setTimeout(function() { renderMenu(option);}, 500);
		}
		
		// 处理高度
		$("#"+opt.elem).height(option.height)
		return new pearMenu(opt);
	}

	pearMenu.prototype.click = function(clickEvent) {
		var _this = this;
		$("body").on("click", ".site-demo-active", function() {
			var dom = $(this);
			var data = {
				menuId: dom.attr("menu-id"),
				menuTitle: dom.attr("menu-title"),
				menuPath: dom.attr("menu-title"),
				menuIcon: dom.attr("menu-icon"),
				menuUrl: dom.attr("menu-url"),
				openType: dom.attr("open-type")
			};
			var doms = hash(dom);
			if (doms != null) {
				if (doms.text() != '') {
					data['menuPath'] = doms.find("span").text() + " / " + data['menuPath'];
				}
			}
			if (doms != null) {
				var domss = hash(doms);
				if(domss!=null){
				if (domss.text() != '') {
					data['menuPath'] = domss.find("span").text() + " / " + data['menuPath'];
				}}
			}
			if (domss != null) {

				var domsss = hash(domss);
				if(domsss!=null){
				if (domsss.text() != '') {
					data['menuPath'] = domsss.find("span").text() + " / " + data['menuPath'];
				}}
			}
			if ($("#" + _this.option.elem).is(".pear-nav-mini")) {
				if (_this.option.accordion) {
					activeMenus = $(this).parent().parent().parent().children("a");
				} else {
					activeMenus.push($(this).parent().parent().parent().children("a"));
				}
			}
			clickEvent(dom, data);
		})
	}

	function hash(dom) {
		var d = dom.parent().parent().prev();
		if (d.prop("tagName") === "UL") {
			return null;
		}
		return d;
	}

	pearMenu.prototype.skin = function(skin) {
		var menu = $(".pear-nav-tree[lay-filter='" + this.option.elem + "']").parent();
		menu.removeClass("dark-theme");
		menu.removeClass("light-theme");
		menu.removeClass("red-theme");
		menu.addClass(skin);
	}

	pearMenu.prototype.selectItem = function(pearId) {
		$("#" + this.option.elem + " a[menu-id]").parents().removeClass("layui-this").removeClass("layui-nav-itemed")
		if (this.option.control != false) {
			$("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".layui-side-scroll ").find("ul").css({
				display: "none"
			});
			$("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".layui-side-scroll ").find(".layui-this").removeClass(
				"layui-this");
			$("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents("ul").css({
				display: "block"
			});
			var controlId = $("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents("ul").attr("pear-id");
			if (controlId != undefined) {
				$("#" + this.option.control).find(".layui-this").removeClass("layui-this");
				$("#" + this.option.control).find("[pear-id='" + controlId + "']").addClass("layui-this");
			}
		}
		if (this.option.accordion == true) {
			$("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".pear-nav-tree").find(".layui-nav-itemed").removeClass(
				"layui-nav-itemed");
		}
		$("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".pear-nav-tree").find(".layui-this").removeClass(
			"layui-this");
		if (!$("#" + this.option.elem).is(".pear-nav-mini")) {
			$("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents(".layui-nav-item").addClass("layui-nav-itemed");
			$("#" + this.option.elem + " a[menu-id='" + pearId + "']").parents("dd").addClass("layui-nav-itemed");
		}
		$("#" + this.option.elem + " a[menu-id='" + pearId + "']").parent().addClass("layui-this");
	}

	var activeMenus;
	pearMenu.prototype.collaspe = function(time) {
		var elem = this.option.elem;
		var config = this.option;
		if ($("#" + this.option.elem).is(".pear-nav-mini")) {
			layer.close(window.miniTips);
			$.each(activeMenus, function(i, item) {
				$("#" + elem + " a[menu-id='" + $(this).attr("menu-id") + "']").parent().addClass("layui-nav-itemed");
			})
			$("#" + this.option.elem).removeClass("pear-nav-mini");
			$("#" + this.option.elem).animate({
				width: "100%"
			}, 150);
			isHoverMenu(false, config);
		} else {
			activeMenus = $("#" + this.option.elem).find(".layui-nav-itemed>a");
			$("#" + this.option.elem).find(".layui-nav-itemed").removeClass("layui-nav-itemed");
			$("#" + this.option.elem).addClass("pear-nav-mini");
			$("#" + this.option.elem).animate({
				width: "60px"
			}, 400);
			isHoverMenu(true, config);
		}
	}

	function getData(url, token) {
		var defer = $.Deferred();
		//$.get(url + "?fresh=" + Math.random(), function(result) {
		//	defer.resolve(result)
		//});
		$.ajax({
			url: url + "?fresh=" + Math.random(),
			beforeSend: function(xhr) {
				if (token !== null){
					xhr.setRequestHeader(token.key, token.token);
				}
			},
			success(result){
				defer.resolve(result)
			}
		})
		return defer.promise();
	}

	function postData(url) {
		var defer = $.Deferred();
		//$.post(url + "?fresh=" + Math.random(), function(result) {
		//	defer.resolve(result)
		//});
		$.ajax({
			url: url + "?fresh=" + Math.random(),
			type: "POST",
			beforeSend: function(xhr) {
				if (token !== null){
					xhr.setRequestHeader(token.key, token.token);
				}
			},
			success(){
				defer.resolve(result)
			}
		})
		return defer.promise();
	}

	function renderMenu(option) {
		if (option.parseData != false) {
			option.parseData(option.data);
		}
		if (option.data.length > 0) {
			if (option.control != false) {
				createMenuAndControl(option);
			} else {
				createMenu(option);
			}
		}
		element.init();
		downShow(option);
		option.done();
	}

	function createMenu(option) {
		var menuHtml = '<div style="height:100%!important;" class="pear-side-scroll layui-side-scroll ' + option.theme + '"><ul lay-filter="' + option.elem +
			'" class="layui-nav arrow   pear-menu layui-nav-tree pear-nav-tree">'
		
		$.each(option.data, function(i, item) {
			var content = '<li class="layui-nav-item" >';
			if (i == option.defaultOpen) {
				content = '<li class="layui-nav-item layui-nav-itemed" >';
			}
			var href = "javascript:;";
			var target = "";
			var className = "site-demo-active"
			if (item[option.fields.openType || "openType"] == (option.fields._blank || "_blank") && item[option.fields.type || "type"] == 1) {
				href = item[option.fields.href || "href"];
				target = "target='_blank'";
				className = "";
			}
			if (item[option.fields.type || "type"] == 0) {
				// 创 建 目 录 结 构
				content += '<a  href="javascript:;" menu-type="' + item[option.fields.type || "type"] + '" menu-id="' + item[option.fields.id || "id"] + '" href="' + href +
					'" ' + target + '><i class="layui-icon ' + (item[option.fields.icon || "icon"] || "layui-icon-component") + '"></i><span>' + item[option.fields.title || "title"] +
					'</span></a>';
			} else if (item[option.fields.type || "type"] == 1) {
				content += '<a class="' + className + '" menu-type="' + item[option.fields.type || "type"] + '" menu-url="' + item[option.fields.href || "href"] + '" menu-id="' +
					item[option.fields.id || "id"] +
					'" menu-title="' + item[option.fields.title || "title"] + '"  href="' + href + '"  ' + target + '><i class="layui-icon ' + (item[option.fields.icon || "icon"] || "layui-icon-component") +
					'"></i><span>' + item[option.fields.title || "title"] + '</span></a>';
			}
			// 调 用 递 归 方 法 加 载 无 限 层 级 的 子 菜 单 
			content += loadchild(item, option.fields);
			// 结 束 一 个 根 菜 单 项
			content += '</li>';
			menuHtml += content;
		});
		// 结 束 菜 单 结 构 的 初 始 化
		menuHtml += "</ul></div>";
		// 将 菜 单 拼 接 到 初 始 化 容 器 中
		$("#" + option.elem).html(menuHtml);
	}

	function createMenuAndControl(option) {
		var control = '<ul class="layui-nav  pear-nav-control pc layui-hide-xs">';
		var controlPe = '<ul class="layui-nav pear-nav-control layui-hide-sm">';
		// 声 明 头 部
		var menu = '<div class="layui-side-scroll ' + option.theme + '">'
		// 开 启 同 步 操 作
		var index = 0;
		var controlItemPe = '<dl class="layui-nav-child">';
		$.each(option.data, function(i, item) {
			var menuItem = '';
			var controlItem = '';
			if (i === option.defaultMenu) {
				controlItem = '<li pear-href="' + item[option.fields.href || "href"] + '" pear-title="' + item[option.fields.title || "title"] + '" pear-id="' + item[option.fields.id || "id"] +
					'" class="layui-this layui-nav-item"><a href="javascript:;">' + item[option.fields.title || "title"] + '</a></li>';
				menuItem = '<ul  pear-id="' + item[option.fields.id || "id"] + '" lay-filter="' + option.elem +
					'" class="layui-nav arrow layui-nav-tree pear-nav-tree">';
				// 兼容移动端
				controlPe += '<li class="layui-nav-item"><a class="pe-title" href="javascript:;" >' + item[option.fields.title || "title"] + '</a>';
				controlItemPe += '<dd  pear-href="' + item[option.fields.href || "href"] + '" pear-title="' + item[option.fields.title || "title"] + '" pear-id="' + item[option.fields.id || "id"] +
					'"><a href="javascript:;">' + item[option.fields.title || "title"] + '</a></dd>';
			} else {
				controlItem = '<li  pear-href="' + item[option.fields.href || "href"] + '" pear-title="' + item[option.fields.title || "title"] + '" pear-id="' + item[option.fields.id || "id"] +
					'" class="layui-nav-item"><a href="javascript:;">' + item[option.fields.title || "title"] + '</a></li>';
				menuItem = '<ul style="display:none" pear-id="' + item[option.fields.id || "id"] + '" lay-filter="' + option.elem +
					'" class="layui-nav arrow layui-nav-tree pear-nav-tree">';

				controlItemPe += '<dd pear-href="' + item[option.fields.href || "href"] + '" pear-title="' + item[option.fields.title || "title"] + '" pear-id="' + item[option.fields.id || "id"] +
					'"><a href="javascript:;">' + item[option.fields.title || "title"] + '</a></dd>';

			}
			index++;
			$.each(item[option.fields.children || "children"], function(i, note) {
				// 创 建 每 一 个 菜 单 项
				var content = '<li class="layui-nav-item" >';
				var href = "javascript:;";
				var target = "";
				var className = "site-demo-active";
				if (note[option.fields.openType || "openType"] == (option.fields._blank || "_blank") && note[option.fields.type || "type"] == 1) {
					href = note[option.fields.href || "href"];
					target = "target='_blank'";
					className = "";
				}
				// 判 断 菜 单 类 型 0 是 不可跳转的目录 1 是 可 点 击 跳 转 的 菜 单
				if (note[option.fields.type || "type"] == 0) {
					// 创 建 目 录 结 构
					content += '<a  href="' + href + '" ' + target + ' menu-type="' + note[option.fields.type || "type"] + '" menu-id="' + note[option.fields.id || "id"] +
						'" ><i class="layui-icon ' + (note[option.fields.icon || "icon"] || "layui-icon-component") + '"></i><span>' + note[option.fields.title || "title"] +
						'</span></a>';
				} else if (note[option.fields.type || "type"] == 1) {
					// 创 建 菜 单 结 构
					content += '<a ' + target + ' class="' + className + '" menu-type="' + note[option.fields.type || "type"] + '" menu-url="' + note[option.fields.href || "href"] +
						'" menu-id="' + note[option.fields.id || "id"] +
						'" menu-title="' + note[option.fields.title || "title"] + '" href="' + href + '"><i class="layui-icon ' + (note[option.fields.icon || "icon"] || "layui-icon-component") +
						'"></i><span>' + note[option.fields.title || "title"] + '</span></a>';
				}
				content += loadchild(note, option.fields);
				content += '</li>';
				menuItem += content;
			})
			menu += menuItem + '</ul>';
			control += controlItem;
		})
		controlItemPe += "</li></dl></ul>"
		controlPe += controlItemPe;
		$("#" + option.control).html(control);
		$("#" + option.control).append(controlPe);
		$("#" + option.elem).html(menu);
		$("#" + option.control + " .pear-nav-control").on("click", "[pear-id]", function() {
			$("#" + option.elem).find(".pear-nav-tree").css({
				display: 'none'
			});
			$("#" + option.elem).find(".pear-nav-tree[pear-id='" + $(this).attr("pear-id") + "']").css({
				display: 'block'
			});
			$("#" + option.control).find(".pe-title").html($(this).attr("pear-title"));
			$("#" + option.control).find("")
			option.change($(this).attr("pear-id"), $(this).attr("pear-title"), $(this).attr("pear-href"))
		})
	}

	/** 加载子菜单 (递归)*/
	function loadchild(obj, option) {
		// 判 单 是 否 是 菜 单， 如 果 是 菜 单 直 接 返 回
		if (obj.type == 1) {
			return "";
		}
		// 创 建 子 菜 单 结 构
		var content = '<dl class="layui-nav-child">';
		// 如 果 嵌 套 不 等 于 空 
		if (obj[option.children ||  "children"] != null && obj[option.children ||  "children"].length > 0) {
			// 遍 历 子 项 目
			$.each(obj[option.children ||  "children"], function(i, note) {
				// 创 建 子 项 结 构
				content += '<dd>';
				var href = "javascript:;";
				var target = "";
				var className = "site-demo-active";
				if (note[option.openType || "openType"] == (option._blank || "_blank") && note[option.type || "type"] == 1) {
					href = note[option.href || "href"];
					target = "target='_blank'";
					className = "";
				}
				// 判 断 子 项 类 型
				if (note[option.type || "type"] == 0) {
					// 创 建 目 录 结 构
					content += '<a ' + target + '  href="' + href + '" menu-type="' + note[option.type || "type"] + '" menu-id="' + note[option.id || "id"] +
						'"><i class="layui-icon ' + (note[option.icon || "icon"] || "layui-icon-component") + '"></i><span>' + note[option.title || "title"] + '</span></a>';
				} else if (note[option.type || "type"] == 1) {
					// 创 建 菜 单 结 构
					content += '<a ' + target + ' class="' + className + '" menu-type="' + note[option.type || "type"] + '" menu-url="' + note[option.href || "href"] +
						'" menu-id="' + note[option.id || "id"] + '" menu-title="' + note[option.title || "title"] + '" menu-icon="layui-icon ' + note[option.icon || "icon"] + '" href="' + href +
						'" ><i class="layui-icon ' + (note[option.icon || "icon"] || "layui-icon-component") + '"></i><span>' + note[option.title || "title"] + '</span></a>';
				}
				// 加 载 子 项 目 录
				content += loadchild(note, option);
				// 结 束 当 前 子 菜 单
				content += '</dd>';
			});
			// 封 装
		} else {
			content += '<div class="toast"> 无 内 容 </div>';
		}
		content += '</dl>';
		return content;
	}

	function downShow(option) {
		$("body #" + option.elem).on("click", "a[menu-type='0']", function() {
			if (!$("#" + option.elem).is(".pear-nav-mini")) {
				var superEle = $(this).parent();
				var ele = $(this).next('.layui-nav-child');
				var heights = ele.children("dd").length * 48;

				if ($(this).parent().is(".layui-nav-itemed")) {
					if (option.accordion) {
						$(this).parent().parent().find(".layui-nav-itemed").removeClass("layui-nav-itemed");
						$(this).parent().addClass("layui-nav-itemed");
					}
					ele.height(0);
					ele.animate({
						height: heights + "px"
					}, 200, function() {
						ele.css({
							height: "auto"
						});
					});
				} else {
					$(this).parent().addClass("layui-nav-itemed");
					ele.animate({
						height: "0px"
					}, 200, function() {
						ele.css({
							height: "auto"
						});
						$(this).parent().removeClass("layui-nav-itemed");
					});
				}
			}
		})
	}

	/** 二 级 悬 浮 菜 单*/
	function isHoverMenu(b, option) {
		if (b) {
			$("body").on("mouseleave", ".popup-tips", function () {
				layer.close(window.miniTips);
			});
			$("body").on("mouseenter", ".popup-tips", function () {
				clearTimeout(window.setTimeCloseTip)
			});

			$("#" + option.elem + ".pear-nav-mini .layui-nav-item,#" + option.elem + ".pear-nav-mini dd").hover(function(e) {
				e.stopPropagation();
				clearTimeout(window.setTimeCloseTip)

				if ($(this).children("dl").length===0){
					window.miniTips = layer.tips($(this).children("a").attr("menu-title"), $(this), {
						//tips: [2],
						time: 300000,
						//skin:"popup-tips",
						success:function (el) {
							var top = $(el).position().top + 9
							$(el).css({ top: top })
						}
					});
					return false
				}

				let tips = $(this).prop("innerHTML")
				tips = "<ul class='layui-nav arrow layui-nav-tree pear-nav-tree layui-this'><li class='layui-nav-item layui-nav-itemed"+($(this).hasClass('layui-this')?" layui-this":"")+"'>"+tips+"</li></ul>"
				window.miniTips = layer.tips(tips, $(this), {
					time: 300000,
					skin:"popup-tips",
					success:function (el) {
						var left = $(el).position().left - 10
						var top = $(el).position().top + 4
						$(el).css({ left:left, top: top })
						element.render()
					}
				});

				//var _this = $(this);
				//_this.siblings().find(".layui-nav-child")
				//	.removeClass("layui-nav-hover").css({
				//		left: 0,
				//		top: 0
				//	});
				//_this.children(".layui-nav-child").addClass("layui-nav-hover");
				//_this.closest('.layui-nav-item').data('time') && clearTimeout(_this.closest('.layui-nav-item').data('time'));
				//var height = $(window).height();
				//var topLength = _this.offset().top;
				//var thisHeight = _this.children(".layui-nav-child").height();
				//if ((thisHeight + topLength) > height) {
				//	topLength = height - thisHeight - 10;
				//}
				//var left = _this.offset().left + 60;
				//if (!_this.hasClass("layui-nav-item")) {
				//	left = _this.offset().left + _this.width();
				//}
				//_this.children(".layui-nav-child").offset({
				//	top: topLength,
				//	left: left + 3
				//});
			}, function(e) {
				e.stopPropagation();
				//var _this = $(this);
				//_this.closest('.layui-nav-item').data('time', setTimeout(function() {
				//	_this.closest('.layui-nav-item')
				//		.find(".layui-nav-child")
				//		.removeClass("layui-nav-hover")
				//		.css({
				//			left: 0,
				//			top: 0
				//		});
				//}, 50));
				window.setTimeCloseTip = setTimeout(function () {
					layer.close(window.miniTips);
				}, 1000)
			})
		} else {
			$("#" + option.elem + " .layui-nav-item").off('mouseenter').unbind('mouseleave');
			$("#" + option.elem + " dd").off('mouseenter').unbind('mouseleave');
		}
	}
	exports(MOD_NAME, new pearMenu());
})
